import fs from "fs/promises";
import path from "path";

async function copyFolder(source: string, target: string) {
  try {
    // 创建目标文件夹
    await fs.mkdir(target, { recursive: true });

    // 获取源文件夹中的所有文件和子文件夹
    const files = await fs.readdir(source);

    // 遍历文件和子文件夹
    for (const file of files) {
      const sourcePath = path.join(source, file);
      const targetPath = path.join(target, file);

      // 判断是否为文件夹
      const stat = await fs.lstat(sourcePath);
      if (stat.isDirectory()) {
        // 如果是文件夹，则递归调用 copyFolder 函数复制子文件夹
        await copyFolder(sourcePath, targetPath);
      } else {
        // 如果是文件，则使用 fs.copyFile 方法复制文件
        await fs.copyFile(sourcePath, targetPath);
      }
    }
    console.log(`Folder ${source} copied to ${target}`);
  } catch (error) {
    console.error(`Error copying folder: ${error}`);
  }
}

// 使用示例
export default copyFolder